/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

// 公共表单验证
export const validateDevice = {
  name: [
    { required: true, message: '请输入设备名称', trigger: 'blur' },
    { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择设备类型', trigger: 'change' }
  ],
  dinterval: [
    { type: 'number', message: '输入值必须为数字值' },
    { validator: (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入下发指令时间'))
      } else {
        const data = Number(value)
        if (data >= 60 && data <= 6000) {
          callback()
        } else {
          callback(new Error('下发指令时间取值在60 ~ 6000之间'))
        }
      }
    }, trigger: 'blur' }
  ],
  reissue: [
    { type: 'number', message: '输入值必须为数字值' },
    { validator: (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入补发次数'))
      } else {
        const data = Number(value)
        if (data >= 3 && data <= 5) {
          callback()
        } else {
          callback(new Error('补发次数取值在3 ~ 5之间'))
        }
      }
    }, trigger: 'blur' }
  ],
  reissueTimeout: [{ type: 'number', message: '输入值必须为数字值' },
    { validator: (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入下发指令超时时间'))
      } else {
        const data = Number(value)
        if (data >= 30 && data <= 300) {
          callback()
        } else {
          callback(new Error('下发指令超时时间取值在30 ~ 300之间'))
        }
      }
    }, trigger: 'blur' }
  ],
  disnormalTimeout: [
    { type: 'number', message: '输入值必须为数字值' },
    { validator: (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入允许设备处于非正常状态时间'))
      } else {
        const data = Number(value)
        if (data >= 15 && data <= 60) {
          callback()
        } else {
          callback(new Error('允许设备处于非正常状态时间取值在15 ~ 60之间'))
        }
      }
    }, trigger: 'blur' }
  ],
  did: [
    { required: true, message: '请输入设备硬件id', trigger: 'blur' },
    { type: 'number', message: '输入值必须为数字值' }
  ]
}

export const validateScene = {
  name: [
    { required: true, message: '请输入场景名称', trigger: 'blur' }
  ],
  address: [
    { required: true, message: '请输入场景地址', trigger: 'blur' }
  ],
  maintainerName: [
    { required: true, message: '请输入场景维护人姓名', trigger: 'blur' }
  ],
  maintainerPhone: [
    { required: true, message: '请输入维护人联系方式', trigger: 'blur' }
  ]
}

export const validateMeasure = {
  controId: [
    { required: true, message: '请选择中控', trigger: 'change' }
  ],
  tranId: [
    { required: true, message: '请选择基准点', trigger: 'change' }
  ],
  receSel: [
    { required: true, message: '请选择监测点', trigger: 'change' }
  ],
  levelId: [
    { required: true, message: '请选择下发周期', trigger: 'change' }
  ]
}

export const validateWarning = {
  type: [
    { required: true, message: '请选择传感器类型', trigger: 'change' }
  ],
  typeselect: [
    { required: true, message: '请选择传感器', trigger: 'change' }
  ],
  distance: [
    { required: true, message: '请输入预警值', trigger: 'blur' }
  ]
}

export const validateLevel = {
  levelName: [
    { required: true, message: '请输入下发等级', trigger: 'blur' }
  ],
  dinterval: [
    { required: true, message: '请输入下发周期时间', trigger: 'blur' }
  ]
}

export const validateExcel = {
  timeSel: [
    { required: true, message: '请选择时间', trigger: 'change' }
  ],
  sceneId: [
    { required: true, message: '请选择场景', trigger: 'change' }
  ],
  type: [
    { required: true, message: '请选择报表类型', trigger: 'change' }
  ]
}

export const validateMessage = {
  userName: [
    { required: true, message: '请输入推送人姓名', trigger: 'blur' }
  ],
  job: [
    { required: true, message: '请输入职位', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入推送号码', trigger: 'blur' }
  ],
  random: [
    { required: true, message: '请输入手机验证码', trigger: 'blur' }
  ],
  level: [
    { required: true, message: '请选择报警等级', trigger: 'change' }
  ],
  sceneId: [
    { required: true, message: '请选择场景', trigger: 'change' }
  ]
}

export const validateWarnDevice = {
  sceneId: [
    { required: true, message: '请选择场景', trigger: 'change' }
  ],
  times: [
    { required: true, message: '请输入时间', trigger: 'blur' }
  ]
}
